// pages/order/creatOrder/product/product.js
import { API_PRODUCT_LIST } from '../../../../sdk/business/url';
import { accDiv, accMul, accAdd, accSub } from '../../../../sdk/utils/operation';
import { notify } from "../../../../sdk/utils/NotifyRegistryCenter.js";
import getIn from "../../../../sdk/utils/getIn";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    productList: [],
    price:0,
    paramsList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    this.fetchData();
  },// 获取车型
  fetchData() {
      global.request(API_PRODUCT_LIST, {cate_nature:2}, (code, res)=>{
          if(code == 'SUCCESS') {
              const paramsList = JSON.parse(getIn(this.options, ['paramsList']));
              const sumPrice = getIn(this.options, ['sumPrice']);
              let productList = getIn(res.data, ['list']);
              productList = productList && productList.map((x) => {
                const sale_attrs = x.sale_attrs.length > 0 ? x.sale_attrs[0].attr_vals : [];
                x.isSelect = false;
                x.linkAttrGroup = x.linkAttrGroup.map(d => {
                  const obj = sale_attrs.find(v => v.attr_val_id == d.attr_val_ids);
                  // 匹配paramsList相应的属性
                  const numObj = paramsList.find(v => v.attr_group_id == d.id);
                  d.group_name = obj ? obj.val_name : d.group_name;
                  d.num = numObj ? numObj.num : 0;
                  return d
                });
                return x
              });
              this.setData({ productList, paramsList, price: +sumPrice });
          }
      })
  },
    // 增加
  addition(e) {
    const { productList, paramsList } = this.data;
    const id = getIn(e.currentTarget.dataset, ['attr_id']);
    const num = getIn(e.currentTarget.dataset, ['num']);
    let until_prict = 0, product_id = '';
    const newProductList = productList && productList.map(x => {
        x.linkAttrGroup = x.linkAttrGroup.map(d => {
          if(d.id == id) {
              d.num = Number(num);
              until_prict = d.group_price;
              product_id = d.product_id;
          }
          return d
        });
      return x
      });
    if (paramsList && paramsList.some && paramsList.some(x => x.attr_group_id == id)){
        const index = paramsList.findIndex(x => x.attr_group_id == id);
        paramsList[index].num = num;
    } else {
        paramsList.push({
            product_id,
            num,
            attr_group_id: id
        })
    }
    this.setData({ productList: newProductList, price: accAdd(this.data.price, until_prict) })
  },
    // 减少
  subtraction(e) {
      const { productList, paramsList } = this.data;
      const id = getIn(e.currentTarget.dataset, ['attr_id']);
      const num = getIn(e.currentTarget.dataset, ['num']);
      if (num <= -1 ) return;
      let until_prict = 0, product_id = '';
      const newProductList = productList && productList.map(x => {
          x.linkAttrGroup = x.linkAttrGroup.map(d => {
              if(d.id == id) {
                  d.num = Number(num);
                  until_prict = d.group_price;
                  product_id = d.product_id;
              }
              return d
          });
          return x
      });
      if (Number(num) != 0){
          const index = paramsList.findIndex(x => x.attr_group_id == id);
          paramsList[index].num = Number(num);
      } else {
          const index = paramsList.findIndex(x => x.attr_group_id == id);
          paramsList.splice(index,1)
      }
      this.setData({ productList: newProductList, price: accSub(this.data.price, until_prict) })
  },
  submit() {
    notify && notify('updateProduct', {paramsList:this.data.paramsList, price: this.data.price})
    global.navigateHelper.navigateBack();
  },
  putDown(e) {
      let { productList } = this.data;
      const id = getIn(e.currentTarget, ['id']);
      productList = productList && productList.map(x => {
          if (x.id == id) {
              x.isSelect = !x.isSelect;
          }
          return x
      })
      this.setData({ productList })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {
  
  }
})